﻿using OneOfAll.Common.Bussiness;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace OneOfAll.Common.Models
{
    public class ScreenModel : BaseModel
    {
        //private byte[,] Matrix;
        private Node[,] Nodes;
        PathHelper ph;
        //private IPathFinder PathFinder = null;
        #region 属性
        private string screenName = string.Empty;
        public string ScreenName
        {
            get { return screenName; }
            set { screenName = value; RaisePropertyChanged(); }
        }

        private int height = 512;
        public int Height
        {
            get { return height; }
            set { height = value; RaisePropertyChanged(); }
        }

        private int width = 512;
        public int Width
        {
            get { return width; }
            set { width = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<BaseObjectModel> objectList = new ObservableCollection<BaseObjectModel>();
        public ObservableCollection<BaseObjectModel> ObjectList
        {
            get { return objectList; }
            set { objectList = value; RaisePropertyChanged(); }
        }
        #endregion

        #region 方法
        public void AddObject(BaseObjectModel info)
        {
            Random rd = new Random(Guid.NewGuid().GetHashCode());
            while(true)
            {
                int x = rd.Next(Width * Global.GlobalResource.GridSize - info.Width) / Global.GlobalResource.GridSize;
                int y = rd.Next(Height * Global.GlobalResource.GridSize - info.Height) / Global.GlobalResource.GridSize;
                if (Nodes[x,y].isWalkable && ObjectList.FirstOrDefault(t => t.X == x && t.Y == y) == null)
                {
                    info.X = x * Global.GlobalResource.GridSize;
                    info.Y = y * Global.GlobalResource.GridSize;
                    ObjectList.Add(info);
                    break;
                }
            }
        }

        public bool CanMove(int x, int y)
        {
            if (x >= Width || x < 0 || y >= Height || y < 0)
                return false;
            return Nodes[x, y].isWalkable ? true : false;
        }

        public List<Point> GetPath(int srcX, int srcY, int desX, int desY)
        {
            List<Point> path = new List<Point>();
            var myResult = ph.FindPath(new Node(srcX / Global.GlobalResource.GridSize, srcY / Global.GlobalResource.GridSize, true), 
                new Node(desX / Global.GlobalResource.GridSize, desY / Global.GlobalResource.GridSize, true));

            //List<PathFinderNode> list = PathFinder.FindPath(new Point(srcX, srcY), new Point(desX, desY)); //开始寻径
            if (myResult != null)
            {
                for (int i = 0; i < myResult.Count; i++)
                {
                    path.Add(new Point(myResult[i].x * Global.GlobalResource.GridSize, myResult[i].y * Global.GlobalResource.GridSize));
                }
            }
            return path.ToList();
        }

        #endregion

        public ScreenModel(int height, int width)
        {
            Height = height;
            Width = width;
            Nodes = new Node[width, height];
            for (int x = 0; x <= Nodes.GetUpperBound(0); x++)
            {
                for (int y = 0; y <= Nodes.GetUpperBound(1); y++)
                {
                    Nodes[x, y] = new Node(x, y, true);
                }
            }
            ph = new PathHelper(Nodes);
        }
    }
}
